package rwt.controlpanel;

import java.io.*;

/**
 * Provides methods for starting and stopping the RWT web server.
 */
public class WebServerController
{
	private WebServerMonitor monitor;
	private String configPath;
	
	public WebServerController(WebServerMonitor monitor)
	{
		this.monitor = monitor;
	}
	
	public void startServer()
	{
		try
		{
			String javaDir = Preferences.getJdkPath();
			File javaDirFile = new File(javaDir);
			if (!javaDirFile.isDirectory()) {
				Util.showError("Your Java JDK installation was not found. Please go to Configuration and make sure the path is correct.");
				return;
			}
			
			File javaExeFile = new File(javaDirFile, "bin/java.exe");
			if (!javaExeFile.isFile()) {
				Util.showError("Java.exe was not found in the bin directory of your Java JDK installation! " +
						"Please go to Configuration and make sure your JDK path is correct.");
				return;
			}
			String javaExePath = javaExeFile.getAbsolutePath();
			
			String gnatDir = Preferences.getGnatAjisPath();
			File gnatJniFile = new File(gnatDir, "bin/gnatjni.dll");
			if (!gnatJniFile.isFile()) {
				Util.showError("Your GNAT AJIS installation is not correct. Please go to Configuration and make sure the path is correct.");
				return;
			}
			File gnatBinDirFile = new File(gnatDir, "bin");
			String gnatBinPath = gnatBinDirFile.getAbsolutePath();
			
			Process p = null;
			if (Preferences.getShowCommandWindow())
			{
				p = new ProcessBuilder("cmd", "/k", "start", "RWT Web Server",
						javaExePath, "-d32", "-Djava.library.path=native\\jni\\lib;" + gnatBinPath,
						"-jar", Constants.WEB_SERVER_JAR_NAME, "-config", getConfigPath())
					.redirectErrorStream(true)
					.start();
			}
			else
			{
				p = new ProcessBuilder(javaExePath, "-d32", "-Djava.library.path=native\\jni\\lib;" + gnatBinPath,
						"-jar", Constants.WEB_SERVER_JAR_NAME, "-config", getConfigPath())
					.redirectErrorStream(true)
					.start();
			}
			
			final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
			
			new Thread() {
				@Override public void run()
				{
					String line = null;
					try
					{
						while ((line = reader.readLine()) != null)
						{
							System.out.println("SERVER:    " + line);
						}
					}
					catch (IOException e)
					{
						e.printStackTrace();
					}
				}
			}.start();
			
			monitor.checkSoon();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void stopServer()
	{
		int pid = monitor.getServerPid();
		System.out.println("Shutting down web server. PID=" + pid + ".");
		
		if (pid > 0)
		{
			try
			{
				Runtime.getRuntime().exec("taskkill /F /PID " + pid);
				monitor.checkSoon();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	public String getConfigPath()
	{
		return configPath;
	}
	
	public void setConfigPath(String configPath)
	{
		this.configPath = configPath;
	}
}
